package com.weigang.jdbctemplate.dao;


import com.weigang.jdbctemplate.entity.HrUser;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class UserDao {

    @Resource
    JdbcTemplate jdbcTemplate;

    public int addUser(HrUser hrUser) {
        return jdbcTemplate.update("INSERT INTO hr_user(name , age) VALUES (? , ?)", hrUser.getName(), hrUser.getAge());
    }

    public int updateUser(HrUser hrUser) {
        return jdbcTemplate.update("UPDATE hr_user SET name= ?,age= ? WHERE id=? ", hrUser.getName(), hrUser.getAge(),hrUser.getId());
    }

    public int deleteUser(Integer id) {
        return jdbcTemplate.update("DELETE FROM hr_user WHERE id=?", id);
    }

    public HrUser getBookById(Integer id) {
        return jdbcTemplate.queryForObject("select * from hr_user where id = ? ", new BeanPropertyRowMapper<>(HrUser.class), id);
    }

    public List<HrUser> getAllBooks() {
        return jdbcTemplate.query("select * from hr_user",new BeanPropertyRowMapper<>(HrUser.class));
    }

}
